package ejb.managerbeans;

import javax.ejb.Stateless;

import ejb.dto.VoloDTO;
import ejb.entities.*;
import ejb.utility.ConvertDTO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class VoloManagerBean
 */
@Stateless
public class VoloManagerBean implements VoloManagerBeanLocal {

	@PersistenceContext
	private EntityManager em;

	@Resource
	private EJBContext context;

	/**
	 * Default constructor.
	 */
	public VoloManagerBean() {

	}

	// public Flight find(String codiceVolo) {
	// return em.find(Flight.class, codiceVolo);
	// }

	public List<Flight> getAllFlights() 
	{
		return em.createNamedQuery(Flight.FIND_ALL, Flight.class ).getResultList();

	}

	@Override
	public List<VoloDTO> getAllVoloDTO() {
		List<VoloDTO> voliDTO = new ArrayList<VoloDTO>();
		List<Flight> flights = getAllFlights();

		for (Flight f : flights)
			voliDTO.add(ConvertDTO.convertToVoloDTO(f));

		return voliDTO;
	}

	@Override
	public List<VoloDTO> getVoloDTODatesCities(VoloDTO volo) {

		List<VoloDTO> voliDTO = new ArrayList<VoloDTO>();

		System.out.println("DTO passato: " + volo.toString());
		List<Flight> flights = em
				.createNamedQuery(Flight.FIND_DATES_CITIES, Flight.class)
				.setParameter("dataInizio", volo.getDataInizio())
				.setParameter("dataFine", volo.getDataFine())
				.setParameter("cittaPartenza", volo.getCitta_partenza())
				.setParameter("cittaArrivo", volo.getCitta_arrivo())
				.getResultList();
		;

		for (Flight f : flights) {
			voliDTO.add(ConvertDTO.convertToVoloDTO(f));
			System.out.println(ConvertDTO.convertToVoloDTO(f).toString());

		}

		return voliDTO;
	}
	
	@Override
	public List<VoloDTO> getVoloDTODatesArrivalCity(Date dataInizio , Date dataFine , String city) {

		List<VoloDTO> voliDTO = new ArrayList<VoloDTO>();

		List<Flight> flights = em
				.createNamedQuery(Flight.FIND_DATES_ARRIVAL_CITY, Flight.class)
				.setParameter("dataInizio", dataInizio)
				.setParameter("dataFine", dataFine)
				.setParameter("cittaArrivo", city)
				.getResultList();
		for (Flight f : flights) {
			voliDTO.add(ConvertDTO.convertToVoloDTO(f));
		}

		return voliDTO;
	}

	@Override
	public List<VoloDTO> getVoloDTODates(VoloDTO volo) {

		List<VoloDTO> voliDTO = new ArrayList<VoloDTO>();

		List<Flight> flights = em
				.createNamedQuery(Flight.FIND_DATES, Flight.class)
				.setParameter("dataInizio", volo.getDataInizio())
				.getResultList();
		;

		for (Flight f : flights) {
			voliDTO.add(ConvertDTO.convertToVoloDTO(f));

		}

		return voliDTO;
	}
	
	@Override
	public VoloDTO getVoloDTOById( int idVolo )
	{
		return ConvertDTO.convertToVoloDTO( em.find( Flight.class, idVolo) );
	}

	// @Override
	// public VoloDTO getVoloDTO(VoloDTO volo) {
	// em.createNamedQuery(Flight.FINDBYDATEPLACEANDGUESTS, Flight.class)
	// .setParameter("id", user.getEmail()
	//
	// return null;
	// }

	/*
	 * public List<User> getAllUsersRegistred() { return em.createNamedQuery( );
	 * }
	 */
}
